import requests
from lxml import etree

url = 'https://ibaotu.com/shipin/'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}

r = requests.get(url=url, headers=headers)

if r.status_code == 200:
    html = etree.HTML(r.text)
    li_list = html.xpath('//ul[@class="clearfix"]/li')
    for li in li_list:
        # .//是指定在当前路径下的所有子孙中找
        # //是指定整个文档的子孙中找
        # 找到所有视频的标题
        video_title = li.xpath('.//span[@class="video-title" or @class="videos-title"]/text()')[0]

        # 找到所有视频的src
        video_src = 'https:' + li.xpath('.//video/@src')[0].replace('.mp4_10s', '')
        print(video_src)
        response = requests.get(url=video_src, headers=headers)
        with open(f'./{video_title}.mp4', 'wb') as file:
            file.write(response.content)
            print('保存成功')

else:
    print('爬取失败')
